package main
import (
	"fmt"
	"net"
	"time"
	"go_code/projectExec2/server/model"
)
//完成对UserDao的初始化任务
func initUserDao(){
	model.MyUserDao = model.NewUserDao(pool)
}

func main(){
	//当main一启动就去初始化redis连接池
	initPool("localhost:6379",16,0,300*time.Second)
	initUserDao()

	//提示信息
	fmt.Println("[新]服务器在8889端口监听...")
	listen,err := net.Listen("tcp","localhost:8889")
	defer listen.Close()
	if err != nil{
		fmt.Println("net.Listen err :",err)
		return
	}
	//一旦监听成功，就等待客户端来连接服务器
	for {
		fmt.Println("等待客户端来连接服务器...")
		conn,err := listen.Accept()
		if err != nil{
			fmt.Println("listen.Accept err :",err)
			continue
		}
		//一旦连接成功，启动一个协程和客户端保持通信
		go process2(conn)
	}
}

//处理和客户端的通信
func process2(conn net.Conn){
	defer conn.Close()
	//调用总控制器
	processor := &Processor{
		Conn : conn,
	}
	err := processor.Process()
	if err != nil{
		fmt.Println("客户端和服务器端协程错误err:",err)
	}
}

